package com.ruoyi.web.controller.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.tzzl.api.PayApi;
import com.ruoyi.tzzl.api.PhoneApi;
import com.ruoyi.tzzl.api.UserSig;
import com.ruoyi.tzzl.domain.WzPayOrderRecord;
import com.ruoyi.tzzl.domain.WzProgramUser;
import com.ruoyi.tzzl.domain.vo.LoginVo;
import com.ruoyi.tzzl.service.IWzPayOrderRecordService;
import com.ruoyi.tzzl.service.IWzProgramUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.ibatis.util.MapUtil;
import org.apache.poi.ss.formula.functions.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author by caoyong
 * @date 2022/12/5
 **/
@Slf4j
@RestController
@RequestMapping("/callback")
public class callbackController extends BaseController {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private IWzProgramUserService userService;
    @Autowired
    private PhoneApi phoneApi;
    @Autowired
    private UserSig userSig;
    @Autowired
    private IWzPayOrderRecordService wzPayOrderRecordService;
    @Autowired
    private IWzProgramUserService wzProgramUserService;

    /**用户登录*/
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginVo loginVo){
        AjaxResult ajax =new AjaxResult();
        //查看短信验证码是否正确
       Object o = redisCache.getCacheObject(loginVo.getSms());
        if (ObjectUtils.isEmpty(o)){
            return ajax.put("sms","验证码错误!");
        }
        // 生成令牌
        String token = loginService.loginWx("tzzl" ,"admin123");
        ajax.put(Constants.TOKEN, token);
       WzProgramUser user = userService.getByPhone(loginVo.getPhone());
        if (ObjectUtils.isNotEmpty(user)){
            ajax.put("user",user);
            ajax.put("code",200);
        }else{
            WzProgramUser user1 =new WzProgramUser();
            List<WzProgramUser> wzPogramUser = wzProgramUserService.list();
           log.info("用户最大的id:{}",wzPogramUser.get(wzPogramUser.size()-1).getId());
            user1.setPhone(loginVo.getPhone()); //手机号码
            Long userId = wzPogramUser.get(wzPogramUser.size()-1).getId();
            Double uId = Double.valueOf(1 +userId);
            log.info("uid为:{}",uId);
            user1.setUserName("用户"+uId); //用户姓名
            user1.setAvatarurl("https://tzz.funongkj.com/profile/upload/2022/12/05/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20221205163903_20221205164010A001.png"); //微信头像
            user1.setCreateTime(new Date());
            userService.save(user1);
            ajax.put("user",userService.getByPhone(loginVo.getPhone()));
            ajax.put("code",200);
        }
        return ajax;
    }

    /**获取手机验证码*/
    @GetMapping("/smsPhone")
    public AjaxResult smsPhone(String phone){
       Object sms = phoneApi.getPhone(phone);
        return AjaxResult.success(sms);
    }

    /**支付宝支付*/
    @GetMapping("/payAlipay")
    public AjaxResult payAlipay(String money, Long wid, Long friend){
      JSONObject jsonObject =  PayApi.fixedPayment(money,wid);
        WzPayOrderRecord wzPayOrderRecord =new WzPayOrderRecord();
        wzPayOrderRecord.setWid(wid); //用户id
        wzPayOrderRecord.setCreateTime(new Date()); //创建时间
        wzPayOrderRecord.setFriend(friend); //友币
        wzPayOrderRecord.setPayAmount(Double.parseDouble(money)); //支付金额
        //付呗订单号
        wzPayOrderRecord.setOrderSn(jsonObject.getString("order_sn"));
        //商家订单号
        wzPayOrderRecord.setTradeNo(jsonObject.getString("trade_no"));
        wzPayOrderRecordService.save(wzPayOrderRecord);
        return AjaxResult.success(jsonObject.getString("qr_code"));
    }

    /**支付回调接口*/
    @PostMapping("/pay")
    public String pay(String data){
        log.info("支付回调接口接收参数:{}",data);
        JSONObject jsonObject =JSONObject.parseObject(data);
        String orderSn = jsonObject.getString("order_sn"); //付呗订单号
        String status = jsonObject.getString("order_status"); //支付状态
        if (status.equals("SUCCESS")){
            //支付成功修改订单状态
          WzPayOrderRecord wzPayOrderRecord =  wzPayOrderRecordService.getByOrderSn(orderSn);
         wzProgramUserService.topUp(wzPayOrderRecord);
        }
        return "success";
    }

    /**微信授权回调地址*/
    @PostMapping("/weChatAuthorization")
    public AjaxResult weChatAuthorization(@RequestBody String authorization){
        log.info("微信授权回调接口接收参数:{}",authorization);
        return AjaxResult.success();
    }

    /**获取用户通讯sig*/
    @GetMapping("/userSig")
    public AjaxResult userSig(String userId){
       Object sig = userSig.genUserSig(userId,2626560);
        return AjaxResult.success(sig);
    }

}


